package com.pubinfo.passbook.common.handler.rabbitmq;

import org.apache.ibatis.type.BigDecimalTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 处理mybatis BigDecimal保留数据小数
 */
@MappedJdbcTypes(JdbcType.DECIMAL)
public class MyBigDecimalTypeHandler extends BigDecimalTypeHandler {
  @Override
  public BigDecimal getNullableResult(ResultSet rs, String columnName) throws SQLException {
    BigDecimal result = super.getNullableResult(rs, columnName) == null ? BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP) : super.getNullableResult(rs, columnName).setScale(2, BigDecimal.ROUND_HALF_UP);
    return result;
  }
  
  @Override
  public BigDecimal getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
    return super.getNullableResult(rs, columnIndex) == null ? BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP) : super.getNullableResult(rs, columnIndex).setScale(2, BigDecimal.ROUND_HALF_UP);
  }
  
  @Override
  public BigDecimal getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
    return super.getNullableResult(cs, columnIndex) == null ? BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP) : super.getNullableResult(cs, columnIndex).setScale(2, BigDecimal.ROUND_HALF_UP);
  }
}